<template>
  <div class="eulogyMessage h100" v-loading="loading">
    <div class="pageContent">
      <el-row :gutter="11" class="fileSearch">
        <el-form :model="ruleForm" :inline="true" ref="ruleForm" label-width="80px">
          <el-form-item label="类别:" prop="state">
            <el-select v-model="ruleForm.contentType" placeholder="请选择类别">
              <el-option label="全部" value=""></el-option>
              <el-option label="留言" value="ly"></el-option>
              <el-option label="悼文" value="dw"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button icon="el-icon-search" type="primary" @click="onSubmit(1)">
              查询
            </el-button>
            <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
      </el-row>
      <el-table
        :data="tableData"
        border
        :header-cell-style="{
          background: 'rgb(246,247,251)',
          color: '#606266',
        }"
        stripe
        class="defaultTableHeight"
      >
        <el-table-column type="index" label="编号" width="50"> </el-table-column>
        <el-table-column prop="operatorVia" align="center" label="用户头像" width="100">
          <template slot-scope="scope">
            <el-image
              class="avatarUrl_img"
              :src="scope.row.operatorVia"
              alt=""
              :preview-src-list="[scope.row.operatorVia]"
            ></el-image>
          </template>
        </el-table-column>
        <el-table-column prop="operatorNick" align="center" label="用户昵称">
        </el-table-column>
        <el-table-column align="center" label="联系方式">
          <template slot-scope="scope">
            {{ scope.row.phone }}
            <el-button v-if="scope.row.phone" size="mini" @click="handleCopy(scope.$index, scope.row)">
              复制
            </el-button>
          </template>
        </el-table-column>
        <el-table-column prop="title" align="center" label="标题"> </el-table-column>
        <el-table-column prop="content" align="center" label="详情" width="600px">
          <template slot-scope="scope">
            <div v-html="scope.row.content"></div>
          </template>
        </el-table-column>
        <el-table-column prop="contentType" align="center" label="类型" width="90">
          <template slot-scope="scope">
            {{ scope.row.contentType === "dw" ? "悼文" : "留言" }}
          </template>
        </el-table-column>
        <el-table-column prop="contentType" align="center" label="创建日期">
          <template slot-scope="scope">
            {{ scope.row.gmtCreate | timestampToTime }}
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="200px">
          <template slot-scope="scope">
            <!-- <el-button type="primary" size="mini">查看</el-button> -->
            <el-button type="danger" size="mini" @click="delInfo(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <p style="text-align: right">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pager.currentPage"
          :page-size="pager.pageSize"
          :page-sizes="[10, 20, 30, 50]"
          background
          layout="total,sizes,prev, pager, next, jumper"
          :total="pager.total"
        >
        </el-pagination>
      </p>
    </div>
  </div>
</template>

<script>
import { note_list, note_delete } from "@/api/appUser/eulogyMessage";
export default {
  name: "eulogyMessage",
  data() {
    return {
      loading: false,
      ruleForm: {
        contentType: "",
      },
      pager: {
        pageSize: 20,
        total: 1,
        currentPage: 1,
      },
      tableData: [],
    };
  },
  mounted() {
    this.onSubmit();
  },
  methods: {
    // 页面加载
    async onSubmit(val) {
      if (val) {
        this.pager.currentPage = 1;
      }
      this.loading = true;
      let postData = {
        contentType: this.ruleForm.contentType,
        pageSize: this.pager.pageSize,
        currentPage: this.pager.currentPage,
      };
      //查询接口
      let res = await note_list(postData);
      this.loading = false;
      if (res.code !== "2000") {
        this.$message.error("数据读取失败");
        return;
      }
      this.tableData = res.data;
      this.pager.currentPage = res.currentPage;
      this.pager.pageSize = res.pageSize;
      this.pager.total = res.total;
    },
    delInfo(item) {
      this.$confirm("此操作将永久删除该数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          this.loading = true;
          let postData = { ids: [item.id] };
          console.log(postData, "postData");
          try {
            const res = await note_delete(postData);
            console.log(res, 200);
            if (res.code === "2000") {
              this.$message.success("删除成功");
              this.onSubmit();
              return;
            }
            this.loading = false;
            this.$message.fail(res.msg || "删除失败");
          } catch (err) {
            this.loading = false;
            console.log(err, 404);
            this.$message.fail(err.msg);
          }
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 点击复制
    handleCopy(index, row) {
      var input = document.createElement("input"); // js创建一个input输入框
      input.value = row.phone; // 将需要复制的文本赋值到创建的input输入框中，this.ruleForm.url这个是我要复制的内容
      console.log(input.value);
      document.body.appendChild(input); // 将输入框暂时创建到实例里面
      input.select(); // 选中输入框中的内容
      document.execCommand("Copy"); // 执行复制操作
      document.body.removeChild(input); // 最后删除实例中临时创建的input输入框，完成复制操作
      this.$message({
        message: "复制成功",
        type: "success",
      });
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.$refs["ruleForm"].resetFields();
      this.onSubmit();
    },
    // 初始页currentPage、初始每页数据数pagesize和数据data
    handleSizeChange: function (size) {
      this.pager.pageSize = size;
      this.onSubmit();
    },
    handleCurrentChange: function (currentPage) {
      this.pager.currentPage = currentPage;
      this.onSubmit(); //点击第几页
    },
  },
};
</script>

<style lang="scss" scoped>
.avatarUrl_img {
  width: 70px;
  height: 70px;
}
</style>
